package fr.orleans.team;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import entities.Staff;
import entities.Team;
import entities.Tournament;
import enums.Flag;
import enums.TournamentStatus;
import fr.orleans.tools.JSFServices;
import fr.orleans.tools.ManagedBeans;

@ManagedBean
@ViewScoped
public class AddFirstStaffTeam extends ManagedBeans{

	private List<SelectItem> listTournamenent;
	
	private Tournament tournamentSelected;

	private Staff oneStaff;
	
	private Team oneTeam;
	
	public List<SelectItem> getListTournamenent() {
		if(this.listTournamenent == null){
			this.listTournamenent = new ArrayList<SelectItem>();
			
			List<Tournament> listTmp = this.getService().getWebAction().getListTournamentByFlagActivatedByTournamentStatus(TournamentStatus.preparation);
			
			for(Tournament tour : listTmp)
				this.listTournamenent.add(new SelectItem(tour, tour.getName()));
			
			if(listTmp.size() > 0)
				this.tournamentSelected = listTmp.get(0);
		}
		return listTournamenent;
	}

	public void setListTournamenent(List<SelectItem> listTournamenent) {
		this.listTournamenent = listTournamenent;
	}

	public Tournament getTournamentSelected() {
		if(this.tournamentSelected == null)
			this.tournamentSelected = new Tournament();
		
		return tournamentSelected;
	}

	public void setTournamentSelected(Tournament tournamentSelected) {
		this.tournamentSelected = tournamentSelected;
	}
	
	public String saveFirstStaff(){
		Set<Staff> setStaff = new HashSet<Staff>();
		this.oneStaff.setTeam(this.getOneTeam());
		this.oneStaff.setOneFlag(Flag.activated);
		this.oneStaff.setTypeStaff("Entraineur");
		setStaff.add(this.oneStaff);
		
		this.getOneTeam().setListStaffMembers(setStaff);
		this.getOneTeam().setOneFlag(Flag.activated);
		this.getOneTeam().setTournament(this.tournamentSelected);
		
		JSFServices.putParameterInRequestMap("team", this.getOneTeam());
		
		return "/modules/team/addTeamManagerStepTwo.jsf";
	}

	public Staff getOneStaff() {
		if(this.oneStaff == null)
			this.oneStaff = new Staff();
		
		return oneStaff;
	}

	public void setOneStaff(Staff oneStaff) {
		this.oneStaff = oneStaff;
	}

	public Team getOneTeam() {
		if(this.oneTeam == null)
			this.oneTeam = new Team();
		return oneTeam;
	}

	public void setOneTeam(Team oneTeam) {
		this.oneTeam = oneTeam;
	}
}